package academic.ui.client.view;


import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.RecentDealsPresenter;
import academic.ui.client.widgets.CompactDeals;
import academic.ui.client.widgets.SliderPanel;
import ecommerce.soco.shared.GWT_Product;


public class RecentDealsView extends ViewImpl implements RecentDealsPresenter.MyView {

	public GenericContainerTag recentDealsDiv = new GenericContainerTag("div");	
	
	public ArrayList<GenericClickable> clickHandlers = new ArrayList<GenericClickable>();
	public SliderPanel slider = new SliderPanel();
	
	@Inject
	public RecentDealsView(CSSConstants css) {
		slider.setAnimationEnabled(true);
		slider.setContent(recentDealsDiv);
		recentDealsDiv.setStyleName("outerContent");
	}
	
	
	@Override
	public Widget asWidget() {
		return slider;
	}


	@Override
	public void setup(List<GWT_Product> result) {
		
		recentDealsDiv.clear();
		
		if(result==null || result.size()<1)
		{
			recentDealsDiv.add(new GenericContainerTag("h3","There are currently no deals that have expired."));
		}
		else
		{
			
			GenericContainerTag dealsDiv = new GenericContainerTag("div");
			dealsDiv.setStyleName("content");
			
			GenericContainerTag wizard = new GenericContainerTag("div");
			GenericContainerTag pages = new GenericContainerTag("div");
			GenericContainerTag back = new GenericContainerTag("a","Previous");
			GenericContainerTag next = new GenericContainerTag("a","Next");
			
			back.setStyleName("prev");
			next.setStyleName("next");
			wizard.getElement().setId("wizard");
			pages.setStyleName("items");
	
			dealsDiv.add(wizard);
			dealsDiv.add(back);
			dealsDiv.add(next);
			wizard.add(pages);
			clickHandlers.clear();
			for(int i=0;i<result.size();i+=6) 
			{
				List<GWT_Product> deals = result.subList(i, Math.min(result.size(), i+6));
				
				GenericContainerTag page = new GenericContainerTag("div");
				pages.add(page);
				page.setStyleName("page");
				CompactDeals recentDealsReport = new CompactDeals(deals);
				page.add(recentDealsReport);
				clickHandlers.addAll(recentDealsReport.getClickHandlers());
			}
			back.addStyleName("disabled");
			
			if(result.size()<=6)
			{
				
				next.addStyleName("disabled");
			}
			else
			{
				next.removeStyleName("disabled");
			}
			
			wizard.setHeight(String.valueOf(Math.min(4,result.size())*165)+"px");
			
			recentDealsDiv.add(dealsDiv);
		}
		slider.setOpen(true);
	}


	@Override
	public ArrayList<GenericClickable> getHandlers() {
		return clickHandlers;
	}
	
	@Override
	public void hide() {
		slider.setOpen(false);
	}
	
	


}
